package com.obas.menus {
	import com.ease.core.components.DockingView;
	import com.ease.core.components.ViewWindow;
	import com.ease.core.components.menus.AbstractMenuProvider;
	import com.ease.core.events.PerspectiveEvent;
	import com.ease.core.managers.PerspectiveManager;

	import mx.events.MenuEvent;

	public class PerspectiveMenuProvider extends AbstractMenuProvider {
		private var perspectiveMenuItem:Object={label: "Open Perspective", children: []};
		private var viewMenuItem:Object={label: "Show View", children: []};
      private var perspectiveManager:PerspectiveManager = PerspectiveManager.getInstance();

		public function PerspectiveMenuProvider() {
			super();
		}

		override public function appendMenuItem(parent:Object):void {
			var activePerspectiveName:String = perspectiveManager.activePerspective;
			var xmlList:Array = perspectiveManager.perspectives;

			for each (var xml:XML in xmlList) {
				perspectiveMenuItem.children.push({
						label: xml.@label,
						perspectiveName: xml.@id,
						icon: xml.@icon.toString(),
						toggled: (activePerspectiveName == xml.@id),
						groupName: "perspectiveGroup",
						type: "radio",
						listener: onPerspectiveMenuItemClick
					});
			}
			parent.children.push(perspectiveMenuItem);
			parent.children.push(viewMenuItem);

			parent.children.push({type: "separator"});
			parent.children.push({label: "Customize Perspective..."});
			parent.children.push({label: "Save Perspective As..."});
			parent.children.push({label: "Reset Perspective..."});
			parent.children.push({label: "Close Perspective"});
			parent.children.push({label: "Close All Perspectives"});
		}

		override public function onMenuItemClick(event:MenuEvent):void {
			switch (event.label) {
				case "Customize Perspective...":
					break;
			}
		}

		private function onPerspectiveMenuItemClick(event:MenuEvent):void {
			event.item.toggled=true;

			var perspectiveEvent:PerspectiveEvent=new PerspectiveEvent(PerspectiveEvent.OPEN);
			perspectiveEvent.perspectiveName=event.item.perspectiveName;
		}

		private function onViewMenuItemClick(event:MenuEvent):void {
		}

		override public function afterPropertiesSet():void {
		}

		private function onPerspectiveOpened(event:PerspectiveEvent):void {
			invalidatePerspectiveMenu(event);
			invalidateViewMenu(event);
		}

		private function invalidatePerspectiveMenu(event:PerspectiveEvent):void {
			var len:int=perspectiveMenuItem.children.length;
			var item:Object;
			for (var ii:int=len - 1; ii >= 0; ii--) {
				item=perspectiveMenuItem.children[ii];
				item.toggled=(event.perspectiveName == item.perspectiveName);
			}
		}

		private function invalidateViewMenu(event:PerspectiveEvent):void {
			var len:int=viewMenuItem.children.length;

			var perspectiveXml:XML = perspectiveManager.getPerspective(event.perspectiveName);
			var views:XMLList=perspectiveXml..View;
			var viewsString:String=views.toXMLString();

			var item:Object;
			for (var ii:int=len - 1; ii >= 0; ii--) {
				item=viewMenuItem.children[ii];
				var tempToggled:Boolean=(viewsString.indexOf("\"" + item.viewName + "\"") > -1);
				item.toggled=tempToggled;
			}
		}

		private function addView():void {
		}
	}
}

